package it.asvp.asvp_portal.service.services.impl;

import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.dao.EventDao;
import it.asvp.asvp_portal.data.dao.EventPictureDao;
import it.asvp.asvp_portal.data.dao.MemberDao;
import it.asvp.asvp_portal.data.entity.event.Event;
import it.asvp.asvp_portal.data.entity.event.EventPicture;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.service.services.EventService;
import it.asvp.asvp_portal.service.services.MemberAccountService;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;


@Service
public class EventServiceImpl implements  EventService{

	@Autowired
	EventDao eventDao;
	@Autowired
	EventPictureDao eventPictureDao;
	@Autowired
	MemberDao memberDao;
	@Autowired
	MemberAccountService memberAccountService;
	
	@Transactional
	@Rollback
	//FIXME: aggiungere il salvataggio delle foto
	public Long addEvent(Long idCurrentAccount,Event transientEvent) throws Exception{
		
		transientEvent.setCreatedOn(DateUtils.getCurrentDate());
		transientEvent.setLastUpdate(DateUtils.getCurrentDate());
		
		Member persistentMember=memberDao.findById(idCurrentAccount);
		transientEvent.setMember(persistentMember);
		return eventDao.persist(transientEvent);
	}
	
	@Transactional
	@Rollback
	public void updateEvent(Long idCurrentAccount, Long idEventToUpdate, Event transientEvent) throws Exception{
		
		Event persistentEvent=eventDao.findById(idEventToUpdate);
		
		if(persistentEvent==null){
			throw new Exception("Evento non presente");
		}
		
		updatePictures(persistentEvent.getId(),persistentEvent.getEventPictures(),transientEvent.getEventPictures());	
		
		persistentEvent.setEventType(transientEvent.getEventType());
		persistentEvent.setLastUpdate(DateUtils.getCurrentDate());
		persistentEvent.setDescription(transientEvent.getDescription());
		persistentEvent.setEndDate(transientEvent.getEndDate());
		persistentEvent.setPlace(transientEvent.getPlace());
		persistentEvent.setPriority(transientEvent.isPriority());
		persistentEvent.setStartDate(transientEvent.getStartDate());
		persistentEvent.setTitle(transientEvent.getTitle());
		
		eventDao.merge(persistentEvent);
	}
	
	@Transactional
	@Rollback
	public void deleteEvent(Long idCurrentAccount, Long idEventToDelete) throws Exception{
		
			Event persistentEvent=eventDao.findById(idEventToDelete);
			
			if(persistentEvent==null){
				throw new Exception("Evento non presente");
			}
			
			eventDao.delete(persistentEvent);	
	}
	
	@Transactional
	@Rollback
	public List<Event> getAllEvents(){
		
		List<Event> allEvents=eventDao.findAll();
		return allEvents;
	}
	
	@Transactional
	@Rollback
	public List<Event> getByPriority(boolean priority){
		return eventDao.findByPriority(priority);
	}
	
	@Transactional
	@Rollback
	public Event getById(Long idEvent) throws Exception{
		
		  Event persistentEvent=eventDao.findById(idEvent);
		  if(persistentEvent==null){
			  throw new Exception("Evento non presente");
		  }
		  return persistentEvent;
	}
	
	@Transactional
	@Rollback
	private void updatePictures(Long idEvent, Set<EventPicture> oldEventPictures,Set<EventPicture> newEventPictures){
		
		for(EventPicture eventPicture:oldEventPictures){
			eventPictureDao.delete(eventPicture);
		}
		
		for(EventPicture eventPicture:newEventPictures){
			eventPicture.setId(idEvent);
			eventPictureDao.persist(eventPicture);
		}
	}
	
}
